package com.guiwu.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guiwu.entity.Category;
import com.guiwu.mapper.CategoryMapper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryService extends ServiceImpl<CategoryMapper, Category> {
    /**
     * 获取当前用户可见的所有分类（系统分类+用户自定义分类）
     */
    public List<Category> getAllCategoriesForUser(Long userId) {
        return this.list(new LambdaQueryWrapper<Category>()
                .and(wrapper -> wrapper.eq(Category::getType, "system")
                        .or().eq(Category::getUserId, userId)));
    }

    /**
     * 新增用户自定义分类
     */
    public boolean addUserCategory(Long userId, String name) {
        Category category = new Category();
        category.setName(name);
        category.setType("user");
        category.setUserId(userId);
        return this.save(category);
    }

    /**
     * 删除用户自定义分类
     */
    public boolean deleteUserCategory(Long userId, Long categoryId) {
        return this.remove(new LambdaQueryWrapper<Category>()
                .eq(Category::getId, categoryId)
                .eq(Category::getUserId, userId)
                .eq(Category::getType, "user"));
    }
} 